package cn.endeavour.basic.controller;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.user.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
@Api(tags = "验证码接口",description = "主要用作验证码")
@CrossOrigin(origins = "*", maxAge = 3600)
public class VerifyCodeController {

    @Autowired
    private IUserService service;

    /**
     * 用于验证码发送
     * @param params 前台填写的电话号码
     * @return
     */
    @PostMapping("/smsCode")
    @ResponseBody
    public AjaxResult sendCode(@RequestBody Map<String, String> params) {
        try {
            // 然后调用方法
            service.sendCode(params);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发送失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }
}
